package com.itheima.mobilesafe69.receiver;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.service.LocationService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;
import android.util.Log;

public class SmsReceiver extends BroadcastReceiver {
	private static final String tag = null;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		//如果接受到,短信发送的广播,则在此次处回去调用此方法
		Log.i(tag, "收到短信,判断短信内容,做相应业务逻辑...........");
		//1,从intent中获取短信广播传递过来的短信数据(多条短信)
		Object[] objects = (Object[]) intent.getExtras().get("pdus");
		//2,遍历获取数组每一条短信,判断其内容是否包含关键字
		for (Object object : objects) {
			//3,将短信对象传递进去,获取一个SmsMessage对象
			SmsMessage sms = SmsMessage.createFromPdu((byte[])object);
			//4,获取发送短信电话号码,短信内容
			String originatingAddress = sms.getOriginatingAddress();//发送电话号码
			String messageBody = sms.getMessageBody();
			
			//5,判断短信内容中是否包含指定的字符串#*alarm*#
			if(messageBody.contains("#*alarm*#")){
				//6,播放报警音乐(MediaPlay),播放音乐文件的资源id
				MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.ylzs);
				mediaPlayer.setLooping(true);
				mediaPlayer.start();
				//7,终止此广播继续向下传播,4.4版本无效了,找到短信数据库,将此条短信内容在数据库里面删除
				abortBroadcast();
			}
			
			if(messageBody.contains("#*location*#")){
				//开启服务,获取经纬度
				context.startService(new Intent(context,LocationService.class));
			}
			
			if(messageBody.contains("#*lockscreen*#")){
				Log.i(tag, "屏幕锁屏");
			}
			
			if(messageBody.contains("#*wipedata*#")){
				Log.i(tag, "清除数据");
			}
		}
	}
}
